const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000


const server = app.listen(8082);
const io = require('socket.io')(server);
require("./socket/socket")(io)


app.use( bodyParser.json({limit:'50mb'}))//限制上传大小
app.use( bodyParser.urlencoded({limit:'50mb',extended:true}))//限制上传大小
app.all('*',function(req,res,next){
  res.header('Access-Control-Allow-Origin', '*');//的允许所有域名的端口请求（跨域解决）
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Methods', '*');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next();
})

// app.use('/data',express.static(__dirname+'/data'));
app.use(express.static(__dirname+'/data'));
//token的判断
//此处写在index.js只要前端发送的请求包含token的数据都会在此处进行判断
//需要写在路由之前
const jwt = require("./config/jwt");//token的加密解密
app.use((req,res,next)=>{
  if(req.body.token != undefined){
    let token = req.body.token;
    let tokenMatch = jwt.verifyToken(token);
    if(tokenMatch === 1){//
      next()
    }else{
      res.send({status:300})
    }
  }else{
    next()
  }
})
const Router  = require("./router/router")
const filesRouter = require("./router/files")
app.use('/',Router)
app.use('/',filesRouter)
app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})